package com.vf.admin.miaodong.service.impl;

import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.dao.AfterMingXiZuanShiDao;
import com.vf.admin.miaodong.dao.AfterUserDao;
import com.vf.admin.miaodong.entity.AppMingXiZuanShi;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.service.AfterMingXiZuanShiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AfterMingXiZuanShiServiceImpl.java
 * @Description 用户明细钻石记录
 * @createTime 2021年11月02日 12:51:00
 */
@Service
public class AfterMingXiZuanShiServiceImpl implements AfterMingXiZuanShiService {

    @Autowired
    AfterMingXiZuanShiDao afterMingXiZuanShiDao;
    @Autowired
    AfterUserDao afterUserDao;

    /**
     * 新增或更新
     * @param appMingXiZuanShi
     */
    @Override
    public void saveOrUpdate(AppMingXiZuanShi appMingXiZuanShi) {
        afterMingXiZuanShiDao.saveOrUpdate(appMingXiZuanShi);
    }

    /**
     * 综合查询
     * @param paraMap
     * @param splitPageBean
     * @return
     */
    @Override
    public List<AppMingXiZuanShi> findList(Map<String, Object> paraMap, SplitPageBean splitPageBean) {
        return afterMingXiZuanShiDao.findList(paraMap, splitPageBean);
    }

    @Override
    public List<AppMingXiZuanShi> findTeamList(Map<String, Object> paramap, SplitPageBean splitPageBean) {
        return afterMingXiZuanShiDao.findTeamList(paramap, splitPageBean);
    }

    @Override
    public Map<String, Object> findTeamListSum(Map<String, Object> paramap) {
        Map<String, Object> data = new HashMap<>();
        List<AppMingXiZuanShi> teamListSum = afterMingXiZuanShiDao.findTeamListSum(paramap);
        double add = teamListSum.stream().filter(e -> e.getLeixing() == 1).mapToDouble(e -> Double.valueOf(e.getNum().stripTrailingZeros().toPlainString())).sum();
        double reduce = teamListSum.stream().filter(e -> e.getLeixing() == 2).mapToDouble(e -> Double.valueOf(e.getNum().stripTrailingZeros().toPlainString())).sum();
        data.put("totalAdd", add);
        data.put("totalReduce", reduce);
        return data;
    }

    /**
     * 添加明细
     * @param userid userid
     * @param num 数量
     * @param leixing 类型  1增加  2减少
     * @param beizhu  备注
     */
    @Override
    public void addMingxi(Integer userid, BigDecimal num, Integer leixing, String beizhu) {

        AppUser user=afterUserDao.get(userid);
        AppMingXiZuanShi zuanShi=new AppMingXiZuanShi();
        zuanShi.setUserid(user.getId());
        zuanShi.setPic(user.getPic());
        zuanShi.setPhone(user.getLoginname());
        zuanShi.setNick(user.getNick());
        zuanShi.setLeixing(leixing);
        zuanShi.setNum(num);
        zuanShi.setCreatetime(new Date());
        zuanShi.setBeizhu(beizhu);
        afterMingXiZuanShiDao.saveOrUpdate(zuanShi);
    }

}